package muilty_thread.java_current_practice_and_kernel.cp14;

public abstract class BaseBoundedBuffer<V> {
    private final V[] buf;
    private int count;
    private int head;
    private int tail;

    public BaseBoundedBuffer(int capacity) {
        buf = (V[]) new Object[capacity];
    }

    protected synchronized void doPut(V v) {
        buf[tail] = v;
        if (++tail == buf.length) {
            tail = 0;

        }
        count++;
    }

    protected synchronized V doTake(){
        V temp = buf[head];
        buf[head] = null;
        if (++head == buf.length) {
            head = 0;
        }
        count--;
        return temp;

    }

    protected synchronized boolean isEmpty(){
        return count == 0;
    }

    protected synchronized boolean isFull(){
        return count == buf.length;
    }



}
